package com.yixiang.np.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.yixiang.np.logaop.AvoidDuplicateSubmission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yixiang.np.annotation.SystemControllerLog;
import com.yixiang.np.constant.GlobalConstant;
import com.yixiang.np.logaop.RequestLimit;
import com.yixiang.np.model.Json;
import com.yixiang.np.model.SessionInfo;
import com.yixiang.np.model.po.User;
import com.yixiang.np.service.ResourceServiceI;
import com.yixiang.np.service.UserServiceI;

@Controller
@RequestMapping("/admin")
public class IndexController extends BaseController {

	@Autowired
	private UserServiceI userService;
	
	@Autowired
	private ResourceServiceI resourceService;

	@RequestMapping("/index")
	public String index(HttpServletRequest request, HttpSession session) {
		SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
		String path = request.getServletContext().getContextPath();
		if ((sessionInfo != null) && (sessionInfo.getId() != null)) {
			//request.setAttribute("resource", resourceService.resourceString(null,sessionInfo.getId()));
			session.setAttribute("resourceFirst", resourceService.resourceFirString(null,sessionInfo.getId(),path));
			session.setAttribute("resourceSecond", resourceService.resourceSecString(null,sessionInfo.getId(), path));
			session.setAttribute("userResource",resourceService.userResource(sessionInfo.getId()));
			return "/index";
		}
		return "/login";
	}

	@RequestLimit(count=5,time=60000)//限制一分钟最多登录五次
	@RequestMapping("/login")
	public void login(User user, HttpSession session,HttpServletRequest request, HttpServletResponse res) throws IOException {
		Json j = new Json();
		try {
			User sysuser = userService.login(user);
			if (sysuser != null) {
				j.setSuccess(true);
				j.setMsg("登陆成功！");

				SessionInfo sessionInfo = new SessionInfo();
				sessionInfo.setId(sysuser.getId());
				sessionInfo.setLoginname(sysuser.getLoginname());
				sessionInfo.setName(sysuser.getName());
				sessionInfo.setMobile(sysuser.getMobile());
				sessionInfo.setEmail(sysuser.getEmail());
				sessionInfo.setOrgId(sysuser.getOrganizationId());
				sessionInfo.setOrgName(sysuser.getOrganizationName());
				sessionInfo.setUsertype(sysuser.getUsertype());
				sessionInfo.setOrgType(sysuser.getOrgType());
				sessionInfo.setState(sysuser.getState());
				sessionInfo.setRemark(sysuser.getRemark());
				// user.setIp(IpUtil.getIpAddr(getRequest()));
				sessionInfo.setResourceList(userService.resourceList(sysuser.getId()));
				//sessionInfo.setResourceAllList(resourceService.resourceAllList());
				session.setMaxInactiveInterval(60*60);//以秒为单位
				session.setAttribute(GlobalConstant.SESSION_INFO, sessionInfo);
			} else {
				j.setMsg("用户名或密码错误！");
			}
			res.setContentType("text/plain;charset=UTF-8");
			PrintWriter printWriter = res.getWriter();
			printWriter.write(JSONObject.toJSONString(j));
		} catch (Exception e) {
			e.printStackTrace();
			res.setContentType("text/plain;charset=UTF-8");
			PrintWriter printWriter = res.getWriter();
			j.setMsg("系统错误！");
			printWriter.write(JSONObject.toJSONString(j));
		}
		
		//return JSONObject.toJSONString(j);
	}

	@ResponseBody
	@RequestMapping("/logout")
	public Json logout(HttpSession session) {
		Json j = new Json();
		if (session != null) {
			session.invalidate();
		}
		j.setSuccess(true);
		j.setMsg("注销成功！");
		return j;
	}

}
